﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Infrastracture.HttpClient;
using ConfApp.Infrastructure;
using ConfApp.Models;
using Microsoft.Phone.Reactive;

namespace ConfApp.Services.Twitter
{
    public class TwitterServiceClient : ITwitterServiceClient
    {
        private const string Query = "http://search.twitter.com/search.json?q=from:mssinergija&rpp=100";
        //private const string Query = "http://search.twitter.com/search.json?q=%23mono&rpp=100";
        public IObservable<TwittBatch> GetNewTwitts(string sinceId)
        {
            var uri = new Uri(Query + (!string.IsNullOrEmpty(sinceId)?"&since_id="+sinceId:""));
            return
                HttpClient
                    .RequestTo(uri)
                    .GetJson<TwitterResultDto>()
                    .Select(dto=>
                                    new TwittBatch()
                                        {
                                            MaxId = dto.max_id_str,
                                            Twitts = dto.results.Select(
                                                t=> new Twitt()
                                                        {
                                                            ID=t.id_str,
                                                            Text = t.text,
                                                            User = t.from_user,
                                                            Date = t.created_at
                                                        }).ToList()
                                        });
        }
    }
}
